package com.fanhao.dllo.soundfm.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.toolbox.ImageLoader;
import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.utils.PlayDialog;
import com.fanhao.dllo.soundfm.volley.VolleySingleton;


/**
 * Created by dllo on 16/1/8.
 * fragment 基类
 */
public abstract class BaseFragment extends Fragment implements View.OnTouchListener {
    private FragmentManager fm;
    private FragmentTransaction ft;
    private PlayDialog dialog;
    private View view;
    private static ImageLoader imageLoader;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dialog = new PlayDialog(getContext(), R.anim.loading_dialog);
        dialog.setCanceledOnTouchOutside(false);
    }

    // 绑定布局
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(getLayoutView(), container, false);
    }

    // 初始化组件
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.view = view;
        view.setOnTouchListener(this);
        imageLoader = VolleySingleton.getInstance()._getImageLoader();
        initView();
    }

    // 简化findViewById
    protected <T extends View> T findView(int resId) {
        T t = (T) view.findViewById(resId);
        return t;
    }

    // 初始化数据
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();

    }



    // 返回值为加载的布局文件
    protected abstract int getLayoutView();

    // 初始化控件
    protected abstract void initView();

    // 初始化数据
    protected abstract void initData();


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }

    // 显示dialog
    protected void showDialog() {
        Log.e("xxx", "进入方法");
        dialog.show();
    }

    // 隐藏dialog
    protected void dismissDialog() {
        dialog.dismiss();
    }

    public static ImageLoader getImageLoader(){
        return imageLoader;
    }

    /**
     * 替换Fragment界面
     *
     * @param id
     * @param fragment
     * @return
     */
    protected Fragment replaceFragment(int id, Fragment fragment) {
        if (fm == null) {
            fm = getFragmentManager();
        }
        ft = fm.beginTransaction();
        // addToBackStack(null) 方法的作用是在替换后不会被finish
        // 好处是 再次返回时,会直接显示原来界面的内容
        ft.replace(id, fragment).addToBackStack(null);
        ft.commit();
        return fragment;
    }

    /**
     * playListFragmet等进入的效果
     */
    public void goToNextPlayList(Fragment fragment) {
        if (fm == null) {
            fm = getFragmentManager();
        }
        ft = fm.beginTransaction();
        ft.setCustomAnimations(
                R.anim.move_in_from_right,
                R.anim.no_move,
                R.anim.no_move,
                R.anim.move_out_from_right
                );
        ft.add(R.id.frameLayout_main, fragment);
        // addToBackStack() 是为了将该Fragment加入到后退栈中
        // 在返回时,可以直接返回到上一个界面
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();
    }

    /**
     * playListFragmet等返回的效果
     */
    public void goBackToRight() {
        if (fm == null) {
            fm = getFragmentManager();
        }
        fm.popBackStack();
        fm.popBackStackImmediate("", 1);

    }

    /**
     *跳转至 playActivity的效果
     */
public void goToNextActivity(Activity activity){


}


}
